﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MpSoft.Library.DAO.Contents;
using MpSoft.Library.DTO;
using MpSoft.Basic.Configuration;

namespace MpSoft.Library.BUSS.Contents
{

    #region  Download
    public class DownloadBUSS
    {
        private const string DOWNLOAD_ALL_KEY = "Mp.download.all-{0}";
        private const string DOWNLOAD_BY_ID_KEY = "Mp.download.id-{0}";

        private static void RemoveByPattern()
        {
            MpCache.RemoveByPattern(DOWNLOAD_ALL_KEY);
            MpCache.RemoveByPattern(DOWNLOAD_BY_ID_KEY);
        }

        public static int Download_Add(Download obj)
        {
            var result = DownloadDAO.Download_Add(obj);
            if (result > 0)
                RemoveByPattern();
            return result;
        }
        public static int Download_Update(Download obj)
        {
            var result = DownloadDAO.Download_Update(obj);
            if (result > 0)
                RemoveByPattern();
            return result;
        }

        public static int Download_Delete(int DownloadID)
        {
            var result = DownloadDAO.Download_Delete(DownloadID);
            if (result > 0)
                RemoveByPattern();
            return result;
        }

        public static Download Download_ByID(int DownloadID)
        {
            string key = string.Format(DOWNLOAD_BY_ID_KEY, DownloadID);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (Download)obj;
            var listNews = Download_List(DownloadID, -1, -1, null, false);
            Download obj2 = null;
            if (listNews != null)
                obj2 = listNews.FirstOrDefault();
            if (obj2 != null)
                MpCache.Max(key, obj2);
            return obj2;
        }

        public static List<Download> Download_ByCategoryID(int CategoryID, int SortOrder)
        {
            string key = string.Format(DOWNLOAD_ALL_KEY, "cid" + CategoryID + "s" + SortOrder);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (List<Download>)obj;
            var listNews = Download_List(-1, CategoryID, SortOrder, null, true);
            MpCache.Max(key, listNews);
            return listNews;
        }

        public static List<Download> Download_List()
        {
            return Download_List(-1, -1, -1, null, false);
        }

        public static List<Download> Download_List(int DownloadID, int CategoryID, int SortOrder, string Title, bool IsActive)
        {
            return DownloadDAO.Download_List(DownloadID, CategoryID, SortOrder, Title, IsActive);
        }
    }
    #endregion

}
